<template>
  <view class="container">
    <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ use: false }" :up="upOption"
      @up="upCallback">

      <!-- tab栏 -->
      <u-tabs :list="tabList" :is-scroll="false" :current="curTab" active-color="#786cff" :duration="0.2"
        @change="onChangeTab" />

      <!-- 列表数据 -->
      <view class="widget-list b-f">
        <view class="widget__detail" v-for="(item, index) in list.data" :key="index">
          <view class="detail__row dis-flex flex-x-between">
            <view class="detail__left f-24">订单号：{{ item.order.order_no }}</view>
            <view class="detail__right f-24 c-violet">
              {{ item.order.state_text }}
            </view>
          </view>
          <view class="detail__row m-top10 dis-flex flex-x-between">
			  
            <view class="detail__left dis-flex flex-y-center">
              <!-- <avatar-image class="user-avatar" :url="item.user.avatar_url" :width="100" :borderWidth="4"
                :borderColor="`#fff`" /> -->
              <view class="user-info dis-flex flex-dir-column flex-x-center">
                <view class="user-nickName f-28">{{ item.user.nick_name }}</view>
                <!-- <view class="user-nickName f-28">***</view> -->
				
                <!-- <view class="user-time f-24 c-80">消费金额：￥{{ item.order_price }}</view> -->
              </view>
            </view>
            <view class="detail__right dis-flex flex-dir-column flex-x-center flex-y-center">
              <view class="detail__money t-r col-m">
                <!-- <text class="f-26">+ </text> -->
				<text class="f-28">消费金额：￥{{ item.order_price }}</text>
                <!-- <text class="f-28">{{ item.my_money }}</text> -->
              </view>
              <view class="detail__time f-22 c-80">{{ item.create_time }}</view>
            </view>
          </view>
		  
		  <view class="goods-list" @click="handleTargetDetail(item.order_id)">
		    <view class="goods-item" v-for="(goods, idx) in item.order_goods" :key="idx">
		      <!-- 商品图片 -->
		      <view class="goods-image">
		        <image class="image" :src="goods.goods_image" mode="scaleToFill"></image>
		      </view>
		      <!-- 商品信息 -->
		      <view class="goods-content">
		        <view class="goods-title twoline-hide"><text>{{ goods.goods_name }}</text></view>
		        <view class="goods-props clearfix">
		          <view class="goods-props-item" v-for="(props, idx) in goods.goods_props" :key="idx">
		            <text>{{ props.value.name }}</text>
		          </view>
		        </view>
		      </view>
		      <!-- 交易信息 -->
		      <view class="goods-trade">
		        <view class="goods-price">
		          <text class="unit">￥</text>
		          <text class="value">{{ goods.is_user_grade ? goods.grade_goods_price : goods.goods_price }}</text>
		        </view>
		        <view class="goods-num">
		          <text>×{{ goods.total_num }}</text>
		        </view>
		      </view>
		    </view>
		  </view>
        </view>
      </view>
    </mescroll-body>
  </view>
</template>

<script>
  import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
  import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
  import AvatarImage from '@/components/avatar-image'
  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
  import * as Api from '@/api/dealer/order'
  import SettingModel from '@/common/model/dealer/Setting'

  const pageSize = 15

  export default {
    components: {
      MescrollBody,
      AvatarImage
    },
    mixins: [MescrollMixin],
    data() {
      return {
        // 选项卡列表
        tabList: [],
        // 当前选项
        curTab: 0,
        // 列表数据
        list: getEmptyPaginateObj(),
        // 上拉加载配置
        upOption: {
          // 首次自动执行
          auto: true,
          // 每页数据的数量; 默认10
          page: { size: pageSize },
          // 数量要大于12条才显示无更多数据
          noMoreSize: 12,
          // 空布局
          empty: {
            tip: '亲，暂无相关数据'
          }
        }
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.getSetting()
    },

    methods: {

      // 获取分销设置
      getSetting() {
        const app = this
        SettingModel.data()
          .then(setting => {
            const words = setting.words.order
            app.setPageTitle(words.title)
            app.setTabList(words.words)
          })
      },

      // 设置页面标题
      setPageTitle(title) {
        uni.setNavigationBarTitle({
          title: title.value
        })
      },

      // 设置选项卡数据
      setTabList(words) {
        const app = this
        app.tabList = [
          // { value: -1, name: words.all.value },
          // { value: 0, name: words.unsettled.value },
          // { value: 1, name: words.settled.value }
        ]
      },

      /**
       * 上拉加载的回调 (页面初始化时也会执行一次)
       * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
       * @param {Object} page
       */
      upCallback(page) {
        const app = this
        // 设置列表数据
        app.getList(page.num)
          .then(list => {
            const curPageLen = list.data.length
            const totalSize = list.data.total
            app.mescroll.endBySize(curPageLen, totalSize)
          })
          .catch(() => app.mescroll.endErr())
      },

      // 获取提现列表
      getList(pageNo = 1) {
        const app = this
        return new Promise((resolve, reject) => {
          Api.list({ settled: app.getTabValue(), page: pageNo })
            .then(result => {
              // 合并新数据
              const newList = result.data.list
              app.list.data = getMoreListData(newList, app.list, pageNo)
              resolve(newList)
            })
        })
      },

      // 获取当前标签项的值
      getTabValue() {
        const app = this
        if (app.tabList.length) {
          return app.tabList[app.curTab].value
        }
        return -1
      },

      // 切换标签项
      onChangeTab(index) {
        const app = this
        // 设置当前选中的标签
        app.curTab = index
        // 刷新订单列表
        app.onRefreshList()
      },

      // 刷新列表数据
      onRefreshList() {
        this.list = getEmptyPaginateObj()
        setTimeout(() => {
          this.mescroll.resetUpScroll()
        }, 120)
      }

    }
  }
</script>

<style>
  page {
    background: #fff;
  }
</style>
<style lang="scss" scoped>
  .c-violet {
    color: #786cff;
  }

  .c-80 {
    color: #808080;
  }

  // 订单列表
  .widget-list {
    padding: 10rpx 20rpx 40rpx 20rpx;
    box-sizing: border-box;
  }

  .widget__detail {
    padding: 20rpx 15rpx;
    box-sizing: border-box;
    font-size: 28rpx;
    border-bottom: 1rpx solid #e7e7e7;

    .user-avatar {
      margin-right: 16rpx;
    }

    .user-info {
      height: 100%;

      .user-nickName {
        margin-bottom: 8rpx;
      }
    }

    .detail__money {
      width: 100%;
      margin-bottom: 8rpx;
    }
  }
  // 商品列表
  .goods-list {
  
    // 商品项
    .goods-item {
      display: flex;
      margin-bottom: 40rpx;
  
      // 商品图片
      .goods-image {
        width: 180rpx;
        height: 180rpx;
  
        .image {
          display: block;
          width: 100%;
          height: 100%;
          border-radius: 8rpx;
        }
      }
  
      // 商品内容
      .goods-content {
        flex: 1;
        padding-left: 16rpx;
        padding-top: 16rpx;
  
        .goods-title {
          font-size: 26rpx;
          max-height: 76rpx;
        }
  
        .goods-props {
          margin-top: 14rpx;
          height: 40rpx;
          color: #ababab;
          font-size: 24rpx;
          overflow: hidden;
  
          .goods-props-item {
            display: inline-block;
            margin-right: 14rpx;
            padding: 4rpx 16rpx;
            border-radius: 12rpx;
            background-color: #F5F5F5;
            width: auto;
          }
        }
  
  
      }
  
      // 交易信息
      .goods-trade {
        padding-top: 16rpx;
        width: 150rpx;
        text-align: right;
        color: $uni-text-color-grey;
        font-size: 26rpx;
  
        .goods-price {
          vertical-align: bottom;
          margin-bottom: 16rpx;
  
          .unit {
            margin-right: -2rpx;
            font-size: 24rpx;
          }
        }
      }
  
    }
  
  }
</style>
